﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls;
using System.Xml.Serialization;

namespace VisualWix
{
    [Serializable]
    public enum HierarchyType
    {
        File,
        Shortcut,
        RootFolder,
        ShortcutRootFolder,
        SystemFolder,
        Folder,
        ShortcutFolder,
        SystemShortcutFolder
    }

    [Serializable]
    public class Item
    {
        protected string id;

        public string ID
        {
            get
            {
                if (itemType == VisualWix.HierarchyType.SystemShortcutFolder || itemType == VisualWix.HierarchyType.SystemFolder)
                {
                    return Name;
                }
                return id;
            }
            set { id = value; }
        }

        protected string name;
        protected HierarchyType itemType;

        public string Name
        {
            get
            { return name; }
            set
            {
                if (name != value)
                {
                    name = value;
                    RaisePropertyChanged("Name");
                }

            }
        }

        public Item ParentItem { get; set; }

        public HierarchyType HierarchyType
        {
            get { return itemType; }
            set { itemType = value; }
        }

        [DefaultValue(true)]
        public bool IsContextMenuEnabled { get; set; }

        void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;
    }

    [Serializable]
    public class FileItem : Item
    {
        public string Source { get; set; }

        [DefaultValue(true)]
        public bool KeyPath { get; set; }

        private string guid;

        public string Guid
        {
            get { return guid; }
            set { guid = value; }
        }


        public FileItem()
        {
            ID = Wix.GetFileId();
            HierarchyType = HierarchyType.File;
        }

        public string GetTargetPath()
        {
            string path = this.Name;
            Item parent = this.ParentItem;

            while (parent != null && parent.ParentItem != null)
            {
                path = string.Format(@"{0}\{1}", parent.Name, path);
                parent = parent.ParentItem;
            }

            return path;
        }
    }

    [Serializable]
    public class ShortcutItem : Item
    {
        public string TargetFile { get; set; }

        public string TargetDirectory { get; set; }

        public string Source { get; set; }

        public string Description { get; set; }

        private string guid;

        public string Guid
        {
            get { return guid; }
            set { guid = value; }
        }

        public ShortcutItem()
        {
            ID = Wix.GetShortcutId();
            HierarchyType = HierarchyType.Shortcut;
        }
    }

    [Serializable]
    public class DirectoryItem : Item
    {
        [DefaultValue(true)]
        public bool IsDragDropEnabled { get; set; }

        public ObservableCollection<Item> Items { get; set; }

        public DirectoryItem()
        {
            ID = Wix.GetDirectoryId();
            Items = new ObservableCollection<Item>();
        }

        public void SetDefaultsById()
        {
            IsContextMenuEnabled = true;
            IsDragDropEnabled = true;

            if (id == "TARGETDIR")
            {
                itemType = VisualWix.HierarchyType.RootFolder;
                Name = id;
            }
            else
            {
                SystemDirectoryType dirType;

                if (Enum.TryParse<SystemDirectoryType>(id, out dirType))
                {
                    itemType = HierarchyType.SystemFolder;
                }
                else
                {
                    itemType = VisualWix.HierarchyType.Folder;
                }
            }
        }
    }

    [Serializable]
    public partial class FileSystemModel
    {

        private FileSystemModelSystemDirectory itemField;

        public FileSystemModelSystemDirectory FilesAndFolders
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }

        private ShortcutRootDirectory shortcutsRoot;

        public ShortcutRootDirectory Shortcuts
        {
            get { return shortcutsRoot; }
            set { shortcutsRoot = value; }
        }


    }

    [Serializable]
    public partial class FileSystemModelSystemDirectory
    {

        private DirectoryItem[] itemField;

        public DirectoryItem[] Directories
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }
    }

    [Serializable]
    public partial class ShortcutRootDirectory
    {

        private DirectoryItem[] itemField;

        public DirectoryItem[] Item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }
    }
}